package com.odm.communitymanage.ui.mine.EditData

import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.SnackbarUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.util.GlideUtils
import com.odm.communitymanage.widget.AvatarModeUtils
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import com.zhihu.matisse.internal.entity.CaptureStrategy
import kotlinx.android.synthetic.main.fragment_avatar.*

/**
 * @description: 头像显示页
 * @author: ODM
 * @date: 2020/4/16
 */
class AvatarFragment : BaseMVVMFragment(){


    override fun initViewModelObserve() {

    }

    override fun initViews() {
        //刷新头像
        refreshAvatar()


        tvExchangeAvatar.setOnClickListener {
            //跳转页面选择图片，当选择了图片后
            Matisse.from(this)
                .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.WEBP))
                .countable(true)
                .maxSelectable(1)
                .capture(true)
                .captureStrategy( CaptureStrategy(true,"com.odm.communitymanage.fileprovider"))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.65f)
                .imageEngine(GlideEngine())
                .showPreview(false)
                .forResult(Constants.REQUEST_IMAGE_CODE)
        }
    }

    private fun refreshAvatar(){
        //从SP中取出头像，先判断是否已经有本地头像加载，再判断是否进行加载默认文字头像
        val avatarUriString = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_AVATAR_URI , "")
        if( avatarUriString != ""){
            //已有存储头像Uri，加载
            GlideUtils.getInstance().loadImageUri(
                requireContext(),
                Uri.parse(avatarUriString),
                ivBigAvatar
            )
        }else {
            val name = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_NAME,"无")
            setNameAvatar(name , ivBigAvatar)
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_avatar
    }

    /**
     * 从图库页面的回调
     * @param requestCode Int
     * @param resultCode Int
     * @param data Intent?
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //加载从 图库中选择的图片,加载进ImageView
        if (requestCode == Constants.REQUEST_IMAGE_CODE && data != null) {
            val uri = Matisse.obtainResult(data)[0]
            //加载图片
            GlideUtils.getInstance()
                .loadImageUri(requireContext(),
                    uri ,
                    ivBigAvatar)
            SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_AVATAR_URI , uri.toString())
            //通知用户成功
            showSuccessSnack(getString(R.string.exchage_success))
        }
    }
}